package D01_03;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo4 {
    public static void main(String[] args) throws IOException {
        Map<String, String> jiexi = jiexi("src/Y2020/D01_03/c.txt");
        save(jiexi);
    }
    public static Map<String,String> jiexi(String s) throws IOException {
        Map<String,String> map = new HashMap<>();
        File file = new File(s);
        List<String> strings = FileUtils.readLines(file);
        for (String str : strings) {
            char[] chars = str.toCharArray();
            for (char c : chars) {
                if (map.containsKey(c)){
                    map.put(c+"",Integer.parseInt(map.get(c))+1+"");
                }
                else {
                    map.put(c+"",1+"");
                }
            }
        }
        return map;
    }
    public static void save(Map<String,String> map) throws IOException {
        for (Map.Entry<String, String> e : map.entrySet()) {
            FileUtils.writeStringToFile(new File("src/Y2020/D01_03/b.txt"),e.getKey()+":"+e.getValue()+"\r\n",true);
        }
    }
}
